<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="培养模式">
              <a-select allowClear v-model="queryParam.pymsm" @change="pymsChange">
                <a-select-option v-for="i in TYPE0347" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12" v-if="queryParam.pymsm === '01'">
            <a-form-item label="门类">
              <a-select allowClear v-model="queryParam.mlxkdm" showSearch :filterOption="filterOption" @change="subjectChange">
                <a-select-option v-for="i in selectMenu.subjectList" :key="i.id" :value="i.xkdm">{{i.xkdm}}-{{ i.xkmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12" v-if="queryParam.pymsm === '01'">
            <a-form-item label="一级学科">
              <a-select allowClear v-model="queryParam.xkdm" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.subSubjectList" :key="i.id" :value="i.xkdm">{{i.xkdm}}-{{ i.xkmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12" v-if="queryParam.pymsm === '02'">
            <a-form-item label="领域">
              <a-select allowClear v-model="queryParam.xkdm" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.subjectList" :key="i.id" :value="i.xkdm">{{i.xkdm}}-{{ i.xkmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="6" :sm="12">
            <a-form-item label="培养来源">
              <a-select allowClear v-model="queryParam.xslxm">
                <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="12">
            <a-form-item label="培养类别">
              <a-select allowClear v-model="queryParam.xslbm">
                <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
<!--          <template v-if="advanced">

          </template>-->
          <a-col :md="8" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="toAdd" style="margin:0 10px">添加模板</a-button>
<!--            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="{show: false}"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="operation" slot-scope="a,b">
          <a @click="showDetail(b)">查看明细</a>
          <a-divider type="vertical" />
          <a @click="toEdit(b)">修改</a>
          <a-divider type="vertical" />
          <a-popconfirm title="是否要删除此行？" @confirm="deleteTemp(b)">
            <a>删除</a>
          </a-popconfirm>
        </template>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {
        pymsm: ''
      },
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '培养方案模板名称',
          dataIndex: 'fambmc'
        },
        // {
        //   title: '门类',
        //   dataIndex: 'mlmc'
        // },
        // {
        //   title: '一级学科',
        //   dataIndex: 'xkmc'
        // },
        {
          title: '培养模式',
          dataIndex: 'pymsmc'
        },
        {
          title: '培养类别',
          dataIndex: 'xslbmc'
        },
        {
          title: '培养来源',
          dataIndex: 'xslxmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '200px',
          scopedSlots: { customRender: 'operation' },
          // fixed: 'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.templateList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0347: state => state.app['TYPE0347'],
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0075: state => state.app['TYPE0075']
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if (from.matched[0] && from.matched[0].name === '静态路由' && pageParams.path === vm.$route.path) {
        vm.pymsChange(pageParams.params.pymsm, 0)
        if (pageParams.params.mlxkdm) {
          vm.subjectChange(pageParams.params.mlxkdm, 0)
        }
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
      } else {
        vm.initSelectMenu('01')
      }
      vm.initDone = true
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.matched[0].name === '静态路由') {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    this.$store.dispatch('app/setDictionary', 'TYPE0347')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
  },
  methods: {
    initSelectMenu (xklb) {
      this.$api.base.subjectAll({xklb}).then(res => {
        this.$set(this.selectMenu , 'subjectList', res.data)
      })
    },
    showDetail (t) {
      this.$router.push(`/cultivate/template/detail/${t.id}`)
    },
    pymsChange(code, reset = 1){
      if (reset) {
        this.$set(this.queryParam, 'xkdm', '')
        this.$set(this.queryParam, 'mlxkdm', '')
      }
      this.initSelectMenu(code === '01' ? '01' : '04')
    },
    subjectChange (code, reset = 1) {
      if (reset) {
        this.$set(this.queryParam, 'xkdm', '')
      }
      if (code) {
        this.$api.base.subjectAll({fjxkdm: code, xklb: '02'}).then(res => {
          this.$set(this.selectMenu, 'subSubjectList', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'subSubjectList', [])
      }
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    toAdd () {
      this.$router.push('/cultivate/template/add')
    },
    deleteTemp (t) {
      this.$api.cultivate.templateDelete(t.id).then(res => {
        this.$message.success('删除成功')
        this.$refs.table.refresh(false)
      })
    },
    toEdit (t) {
      this.$router.push(`/cultivate/template/add?id=${t.id}`)
    }
  }
}
</script>

<style>

</style>
